/*
 * Job.cpp
 *
 *  Created on: Aug 5, 2013
 *      Author: alexandru
 */

#include "Job.h"

namespace stn {

Job::Job() {
	// TODO remove this class


}

Job::~Job() {

}




std::string Job::runCommand(std::string input){
	using namespace std;
	cout << "scanning  "<< input << "in object "<< getName() << endl;
	if(input=="jsonize"){
		return toJson();
	}


	if(input.at(0)=='#'){
		return "LOAD FILE";
	}



	bool isDin=false;
	bool isNum=false;
	bool putProp=true;

	string prop;
	string restToScan;



	for (unsigned i=0; i<input.length(); ++i)
					  {
						char c=input.at(i);



						switch(c){
							case '.':
								restToScan=input.substr(prop.length()+1, input.length());
								goto returnAsObject;
							break;


							case '[':
								putProp=false;
								isDin=true;
								break;

							case ']':
								putProp=false;
								isDin=true;
							break;

							default:

							break;
						}


						prop+=c;


					  }//exit for












		returnAsObject:
		cout << "input.length()=" << input.length()<< " "<< input << endl;
		cout << "str.length()="<< prop.length() << " " << prop<< endl;
		cout << "restToScan.length()=" << restToScan.length()<< " "<< restToScan << endl;




			if(restToScan.length()>0){

				Object obj= getByName(prop);

				std::cout << prop << std::endl;
				return  obj.runCommand(restToScan);
				return "Syntax error";
			}else{
				cout << "ELSE STATEMENT" << endl;
				return this->toJson();
			}


		return "Syntax error";


		if(!isDin){
			cout << "object is not dinamic" << endl;
			if(prop.length()!=input.length() && prop.length()>0){// daca are restToGet
				restToScan=input.substr(prop.length()+1, input.length());
				cout << " ... continue recursive with name "<< prop << " restToScan= " << restToScan << " length:" << restToScan.length() << endl;
				//
				cout << " continue recursive in object "   << endl;
				//
				return "unimplemented";
			}
		}else{
			cout << "object IS dinamic" << endl;

			if(prop.length()>0){
				cout << "object has name"<< prop << "  "<< prop.length() << endl;
				restToScan=input.substr(prop.length(), input.length()-1);

				cout << " continue recursive in object "  << endl;
				return "unimplemented";
			}else{
				cout << "object has no name"<< prop << "  "<< prop.length() << endl;
				cout << "retrun to input: "<< input << endl;
				string::size_type loc1 = input.find('[', 0 );
				string::size_type loc2 = input.find(']', 0 );
				prop=input.substr(loc1+1, loc2-1);
				cout << "between brackets:  "<< prop << endl;
				restToScan=input.substr(loc2+1, input.length()-1);

				if(restToScan.length()>0){
					cout << " ... continue recursive with name "<< prop << " restToScan= " << restToScan << " length:" << restToScan.length() << endl;
					return "unimplemented";;
				}else{
					return "unimplemented";
				}

			}

		}


	return "Syntax error";
}

} /* namespace stn */
